<? require("adm_header.php"); ?>
<? 
writeCommonAdminTable(LA_ADMIN_USER_ADMINISTRATION,1);
$tplPackages = new tplLoad();
$lLimit = 15;

if (empty($offset))
	$offset=0;

if (getParam("validated","")=="")
{
	$tplPackages->assign("show_searchform",1);
	$arrList[1]=LA_ADMIN_USER_NOT_SEEN_M. " 1 " . LA_ADMIN_MONTHS;
	$arrList[3]=LA_ADMIN_USER_NOT_SEEN_M. " 3 " . LA_ADMIN_MONTHS;
	$arrList[6]=LA_ADMIN_USER_NOT_SEEN_M. " 6 " . LA_ADMIN_MONTHS;
	$arrList[12]=LA_ADMIN_USER_NOT_SEEN_M. " 12 " . LA_ADMIN_MONTHS;
	$arrList[24]=LA_ADMIN_USER_NOT_SEEN_M. " 24 " . LA_ADMIN_MONTHS;
	$tplPackages->assign("notseenlist",getDDFromList($arrList,getParam("last_login_before","")));
	$arrListAds[99]=LA_ADMIN_NO_ADS;
	$arrListAds[90]=LA_ADMIN_EXACTLY. " 1 ".  LA_ADMIN_ADS;
	$arrListAds[91]=LA_ADMIN_EXACTLY. " 2 ".  LA_ADMIN_ADS;
	$arrListAds[92]=LA_ADMIN_EXACTLY. " 3 ".  LA_ADMIN_ADS;
	$arrListAds[92]=LA_ADMIN_EXACTLY. " 4 ".  LA_ADMIN_ADS;
	$arrListAds[92]=LA_ADMIN_EXACTLY. " 5 ".  LA_ADMIN_ADS;
	$arrListAds[1]=LA_ADMIN_MORE_THAN . " 1 " . LA_ADMIN_ADS;
	$arrListAds[2]=LA_ADMIN_MORE_THAN . " 2 " . LA_ADMIN_ADS;
	$arrListAds[3]=LA_ADMIN_MORE_THAN . " 3 " . LA_ADMIN_ADS;
	$arrListAds[4]=LA_ADMIN_MORE_THAN . " 4 " . LA_ADMIN_ADS;
	$arrListAds[5]=LA_ADMIN_MORE_THAN . " 5 " . LA_ADMIN_ADS;
	$tplPackages->assign("numberofadslist",getDDFromList($arrListAds,getParam("number_of_ads","")));
}
	
		
if (getParam("delete","")>0)
{
	
	$tplPackages->assign("deleted");
	$tplPackages->assign("name",getParam("edit_title",""));
	
	if (getParam("confirmed","")==1)
	{
		delete_user(getParam("delete",""));
		
		$tplPackages->assign("delete_completed",getParam("delete",""));
	}
	else 
	{
		$tplPackages->assign("delete_confirm",getParam("delete",""));
	}
}


if (getParam("validated","")==2)
{
	if ($set_approve_mem!=1)
	{
		$lStop=1;
		$tplPackages->assign("msg","<b class='warning'>".LA_ADMIN_MEMBER_VAL_NOT_ACTIVATED."</b><br />".LA_ADMIN_MEMBER_YOU_MAY." <a href='adm_set.php?file=opt_inc.php#set_approve_mem'>".LA_ADMIN_MEMBER_ACTIVATE_IT."</a>");
		$tplPackages->assign("hide",1);
	}
	else 
		$tplPackages->assign("msg","<b>".LA_ADMIN_SHOWING_NOT_VALIDATED."</b>");
}

if (getParam("make_vendor",""))
{
	if (getParam("make_vendor","") == 1)
		q("update $usr_tbl set user_is_vendor = 1 where user_id = ". getParam("edit_user",""));
	else if (getParam("make_vendor","") == 2) 
		q("update $usr_tbl set user_is_vendor = 0 where user_id = ". getParam("edit_user",""));
} 


if (getParam("activate","") && wa())
{
	$lApproveUserID=getParamInt("activate","");
	$sql_app = "update $usr_tbl set user_approved = 1 where user_id = '$lApproveUserID'";
	$result = q($sql_app);
	
	$sql="select user_name,user_email from $usr_tbl where user_id='". $lApproveUserID. "'";
	$resact=q($sql);
	$row=mysql_fetch_array($resact);
	
	$lName=$row["user_name"];
	$lEmail=$row["user_email"];
	
	$tplPackages->assign("msg","User <a href='mailto:$lEmail'><strong>$lName</strong></a> has been granted approval");
	
	if ($set_inform_user_user_valid)
	{
		if ($lName && $lEmail && $lApproveUserID)
		{
			$lNewAdArray=array("$lName","$lEmail","","$set_sitename","","http://$set_url/member_login.php","","");
			
			$lSubjectText=getLangString($set_language,"LA_MAIL_APPROVE_MEM_SUBJECT");
			$lBodyText=getLangString($set_language,"LA_MAIL_APPROVE_MEM_BODY");
			
			$lSubject = formatString($lSubjectText, $lNewAdArray);
			$lBody = formatString($lBodyText, $lNewAdArray);
			
			sendEmail($lEmail,$set_webmaster_address, $lSubject, $lBody);
		}
			
		
		
		
	}       	
}


if (!$lStop)
{
$lFind=getParam("find","");
$lNumberOfAdsRequires=getParam("number_of_ads","");
$lSearchLimiter="";

	
if (getParam("find",""))
{
	$lFind=strToDb($lFind);
	$lSearchLimiter.=" AND (user_id like '%$lFind%' OR user_name like '%$lFind%' OR user_email like '%$lFind%')";	
	$tplPackages->assign("find",$lFind);
}
if (getParam("ads_by_user",""))
	$lBuildString.=" AND (ad_owner = '". getParam("ads_by_user","") . "')";

if (getParam("ads_by_cat",""))
	$lSearchLimiter.=" AND (ad_cat_id = '". getParam("ads_by_cat","") . "')";
	
if (getParam("ad_id_show",""))
	$lSearchLimiter.=" AND (ad_id = '". getParam("ad_id_show","") . "')";

if (getParam("validated","")==2)
	$lSearchLimiter.=" AND (user_approved=0)";	
	
if (getParam("last_login_before",""))
	$lSearchLimiter.=" AND (user_date_last_login<".getDateSubstMonths(time(),getParam("last_login_before","")).") ";

	
if ($lNumberOfAdsRequires)
{
	
	if ($lNumberOfAdsRequires>=90)
	{	
		if ($lNumberOfAdsRequires==99)
			$lSearchLimiter.=" AND (user_num_ads=0 OR user_num_ads IS NULL) ";
		elseif ($lNumberOfAdsRequires==90) 
			$lSearchLimiter.=" AND (user_num_ads=1) ";
		elseif ($lNumberOfAdsRequires==91) 
			$lSearchLimiter.=" AND (user_num_ads=2) ";
		elseif ($lNumberOfAdsRequires==92) 
			$lSearchLimiter.=" AND (user_num_ads=3) ";
		elseif ($lNumberOfAdsRequires==93) 
			$lSearchLimiter.=" AND (user_num_ads=4) ";
		elseif ($lNumberOfAdsRequires==94) 
			$lSearchLimiter.=" AND (user_num_ads=5) ";
	}
	else 
		$lSearchLimiter.=" AND (user_num_ads>=$lNumberOfAdsRequires) ";
}	


// Get total count
$sql_select = "select count(user_id) as num_rows from $usr_tbl where 1<2 $lSearchLimiter";  
$num_res=q($sql_select);
$lNumAds_row = mysql_fetch_array($num_res);
$lNumAds=$lNumAds_row["num_rows"];
$tplPackages->assign("url_vendor",getAllParams(array("make_vendor","edit_user","make_premium")));
$tplPackages->assign("url_delete",getAllParams(array("delete","confirmed")));
$tplPackages->assign("url_approve",getAllParams(array("activate")));
$retArray=urlBar($lNumAds,$lLimit,getParam("page",1),getAllParams(array("page","delete","make_vendor")));
$sql_limit=$retArray["sql_limit"];

$tplPackages->assign("num_ads",$lNumAds);
$sql_select = "select * from $usr_tbl where 1<2 $lSearchLimiter order by " . getParam("orderby","user_id") . "  " .getParam("orderway","desc") . " $sql_limit";  
$result = q ($sql_select);


//print "<p>Search matched <b>$lNumUsers</b> users, showing <b>$lNumCurrentScreen</b> in this screen</p>";
$i=0;	
while ($row = mysql_fetch_array($result))
{
	$credits=0;
	$num_logged=0;
	$user_id = $row["user_id"];
	$name = $row["user_name"];
	$user_name = $row["user_name"];
	$email = $row["user_email"];
	$user_num_ads = $row["user_num_ads"];
	$credits = $row["credits"];
	$status = $row["user_status"];
	$user_approved = $row["user_approved"];
	$user_num_login = $row["user_num_login"];
	$user_is_vendor = $row["user_is_vendor"];
	$user_date=formatDateShort($row["user_date"]);
	$user_date_last_login=formatDateShort($row["user_date_last_login"]);
	$color=colorSwitch($color);	

	$lUserArray[$i]["cat_id"]=$cat_id;
	$lUserArray[$i]["user_num_ads"]=$user_num_ads;
	$lUserArray[$i]["user_num_login"]=$user_num_login;
	$lUserArray[$i]["cat_name"]=dotString($cat_name,18);
	$lUserArray[$i]["ad_description"]=strEnc($ad_description);
	$lUserArray[$i]["user_name"]=strEnc(dotString($user_name,150));
	$lUserArray[$i]["user_id"]=$user_id;
	$lUserArray[$i]["user_is_vendor"]=$user_is_vendor;
	$lUserArray[$i]["user_approved"]=$user_approved;

	$lUserArray[$i]["user_date"]=$user_date;
	$lUserArray[$i]["user_date_last_login"]=$user_date_last_login;
	$lUserArray[$i]["color"]=$color;
	$lUserArray[$i]["NB"]=$i;
    $i++;
    
	}
	$tplPackages->assign("url_bar",$retArray["url_bar"]);
}


if ($set_approve_mem)
	$tplPackages->assign("approve_mem",1);
$tplPackages->assign("listArray",$lUserArray);
$tplPackages->display("adm_user_list.tpl");
writeCommonAdminTable("User Administration","");
require("adm_footer.php"); 
?>
